package com.jwds.digital.bios.business.service.service.belt.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.BeltPerformanceHourReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltPerformanceHourRespVO;
import com.jwds.digital.bios.business.service.service.belt.IBeltPerformanceMonthService;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.BeltPerformanceMonthReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltPerformanceMonthRespVO;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.assembler.BeltPerformanceMonthAssembler;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltPerformanceMonth;
import com.jwds.digital.bios.business.core.infrastructure.mapper.belt.BeltPerformanceMonthMapper;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.jwds.digital.bios.business.service.service.belt.impl.BeltPerformanceHourServiceImpl.getHourRespVO;
import static com.jwds.digital.bios.business.service.service.belt.impl.BeltPerformanceHourServiceImpl.getTimeList;

/**
 * 皮带效能月表实现类
 * @author 林柏云
 * @date 2024-10-24
 */
@Slf4j
@Service
public class BeltPerformanceMonthServiceImpl extends ServiceImpl<BeltPerformanceMonthMapper, BeltPerformanceMonth> implements IBeltPerformanceMonthService {

  @Autowired
  private BeltPerformanceMonthMapper baseMapper;

  @Override
  public BeltPerformanceMonthRespVO queryById(Long id) {
    if(id == null) {
      return null;
    }
    BeltPerformanceMonth entity = baseMapper.selectById(id);
    return BeltPerformanceMonthAssembler.entity2dto(entity);
  }

  @Override
  public PageResponse<BeltPerformanceMonthRespVO> page(PageRequest<BeltPerformanceMonthReqDTO> request) {
    BeltPerformanceMonthReqDTO condition = request.getQueryCondition();
    Page<BeltPerformanceMonthRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
    IPage<BeltPerformanceMonthRespVO> pageData = baseMapper.pageQuery(page, condition);
    return new PageResponse<>((Page<?>)pageData, pageData.getRecords());
  }

  @Override
  public List<BeltPerformanceMonthRespVO> list(BeltPerformanceMonthReqDTO reqDto) {
    return baseMapper.queryList(reqDto);
  }

  @Override
  public void add(BeltPerformanceMonthReqDTO reqDto) {
    BeltPerformanceMonth entity = BeltPerformanceMonthAssembler.dto2entity(reqDto);
    baseMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchAdd(List<BeltPerformanceMonthReqDTO> dtoList) {
    dtoList.forEach(this::add);
  }

  @Override
  public void update(BeltPerformanceMonthReqDTO reqDto) {
    BeltPerformanceMonth oldEntity = baseMapper.selectById(reqDto.getId());
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    BeltPerformanceMonth newEntity = BeltPerformanceMonthAssembler.dto2entity(reqDto, oldEntity);
    baseMapper.updateById(newEntity);
  }

  @Override
  public void delete(Long id) {
    BeltPerformanceMonth oldEntity = baseMapper.selectById(id);
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    baseMapper.deleteById(id);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchDelete(List<Long> ids) {
    for (Long id : ids) {
      delete(id);
    }
  }

  @Override
  public BeltPerformanceHourRespVO analyze(BeltPerformanceHourReqDTO reqDto) {
    // 取开始和结束时间，保留小时及以上单位时间
    //    reqDto.setStartTime(reqDto.getStartTime().withMinute(0).withSecond(0).withNano(0));
    //    reqDto.setEndTime(reqDto.getEndTime().withMinute(0).withSecond(0).withNano(0));
    reqDto.setEndTime(LocalDateTime.now());
    reqDto.setStartTime(reqDto.getEndTime().minusMonths(1));
    // 获取上月开始时间（1号 00:00:00）
    LocalDateTime startOfMonth = reqDto.getStartTime()
            .with(TemporalAdjusters.firstDayOfMonth())
            .withHour(0).withMinute(0).withSecond(0).withNano(0);

    // 获取本月结束时间（最后一天 23:59:59）
    LocalDateTime endOfMonth = reqDto.getEndTime()
            .with(TemporalAdjusters.lastDayOfMonth())
            .withHour(23).withMinute(59).withSecond(59).withNano(0);
    BeltPerformanceHourRespVO beltPerformanceHourRespVO = baseMapper.analyze(reqDto.getId(), startOfMonth, endOfMonth);
    List<LocalDateTime> timeList = getTimeList(reqDto.getStartTime().minusDays(30*7), reqDto.getEndTime(), ChronoUnit.MONTHS);
    // 每个时间段的负载率和空载负载率
    return  getHourRespVO(reqDto, timeList, beltPerformanceHourRespVO,baseMapper,ChronoUnit.MONTHS);
  }

}